// bind 内部返回一个函数

function foo(x, y) {
  console.log(this.a, x, y);
}

const obj = {
  a: 1
}

const res = foo.bind(obj, 2)
res(3)  // 1 2 3

Function.prototype.myBind = function (context, ...args) {
  if (typeof this !== 'function') {
    throw new TypeError("Error")
  }

  const fn = this

  return function Fn(...arg) {
    const arr = args.concat(arg)
    fn.apply(context, arr)
  }
}

const res1 = foo.myBind(obj, 2)
res1(3)  // 1 2 3
